#include "NetCal.hpp"
#include "Protocol.hpp"
#include "TcpServer.hpp"
#include "Daemon.hpp"
#include <unistd.h>

int main(int argc, char *argv[])
{
    // Usage: ./TcpServer port
    if (argc != 2)
    {
        std::cerr << "Usage:" << argv[0] << " port" << std::endl;
        exit(USAGE_ERR);
    }

    std::cout<<"服务器已经启动，成为了一个守护进程"<<std::endl;
    Daemon(0,0);
    //daemon(0,0);

    Enable_File_Log_Strategy();

    // 1.顶层
    std::unique_ptr<Cal> cal = std::make_unique<Cal>();

    // 2.协议层
    std::unique_ptr<Protocol> protocol = std::make_unique<Protocol>([&cal](Request &req) -> Response
                                                                    { return cal->Execute(req); });

    // 3.服务器层
    std::unique_ptr<TcpServer> tsvr = std::make_unique<TcpServer>(std::stoi(argv[1]), [&protocol](std::shared_ptr<Socket> sock, InetAddr &client)
                                                                  { protocol->GetRequest(sock, client); });

    tsvr->Start();

    return 0;
}